<?php

class Main extends Controller {

    function Main() {
        parent::Controller();
        $this->load->model('model_data');
        //verificare sesiune
        if (!$this->usersession->userLoggedIn()) {
            redirect("login/index/", "refresh");
        }
    }

    function index() {

        $time_start = microtime(true);

        $data['user'] = $this->usersession->currentUser();

        $data['extCity'] = $this->model_data->bringExt('city');
        $data['extCounty'] = $this->model_data->bringExt('county');
        $data['extDomain'] = $this->model_data->bringExt('domain');
        $data['extSubDomain'] = $this->model_data->bringExt('domain', 1);
        $data['extPosition'] = $this->model_data->bringExt('position');
        $data['extSubPosition'] = $this->model_data->bringExt('position', 1);
        $data['extClient'] = $this->model_data->bringExt('client');
        $data['extDivision'] = $this->model_data->bringExt('division');

        $data['extUser'] = $this->model_data->bringUser();
        $data['extLetters'] = $this->model_data->bringLetters();

        $data['extAge'] = $this->model_data->bringExtCustom('age');
        $data['extUnsubscribed'] = $this->model_data->bringExtCustom('unsubscribed');
        $data['extIncome'] = $this->model_data->bringExtCustom('income');
        $data['extEmployee'] = $this->model_data->bringExtCustom('employee');
        $data['extAvailable'] = $this->model_data->bringExtCustom('available');
        $data['extPriority'] = $this->model_data->bringExtCustom('priority');
        $data['extSex'] = $this->model_data->bringExtCustom('sex');
       // $data['extTurnover'] = $this->model_data->bringExtCustom('turnover');

        $time_end = microtime(true);
        $time = $time_end - $time_start;
        //echo "It took $time seconds\n";

        $this->load->view('main', $data);
    }

    function do_logout() {
        $this->usersession->logoutUser();
        header("Location: " . $this->config->item('base_url') . "login");
    }


    /** Salvare logo
     * @return void
     */
    function saveLogo() {

        $field_name = "file";
        $config['upload_path'] = $this->config->item('upload_logo');
        $config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|JPG|PNG|JPEG';
        $config['remove_spaces'] = TRUE;
        $config["file_name"] = "logomtp";
        $config['overwrite'] = TRUE;
        $this->upload->initialize($config);

        if (!$this->upload->do_upload($field_name)) {
            $aResponse = array("error" => TRUE, "description" => "Probleme la upload", "success" => FALSE);
            echo json_encode($aResponse);
        }
        else {
            $aFile = $this->upload->data();
            $aResponse = array("error" => false, "description" => "Datele au fost salvate", "success" => true);
            $this->db->where("code", "logo");
            $this->db->update("settings", array("value" => $aFile["file_name"]));

            echo json_encode($aResponse);
        }
    }

    function getLogo() {
        $this->db->where("code", "logo");
        $sLogo = $this->db->get("settings")->row()->value;
         $sLogo = "resources/logo/". $sLogo;
        $aResponse = array("error" => FALSE, "description" => $sLogo, "success" => TRUE);
        echo json_encode($aResponse);
    }
    
	function script() {
		$aCifraAfaceri = array("turnover","turnover2010", "turnover2011");
		$aCifre = array (
			array("0",0),
			array("10",0),
			array("20", "6500"),
			array("30", "13000 "),
			array("40", "67500"),	
			array("50", "132500"),
			array("60", "553000"),
			array("70", "1325000"),
			array("80", "2000000"),
			array("90", "2650000"),
			array("100", "3900000"),
			array("110", "5500000"),
			array("120", "6650000"),
			array("130", "13300000"),
			array("140", "26500000"),
			array("150", "40500000"),
			array("160", "66500000"),
			array("170", "133000000"),
			array("180", "202000000"),
			array("190", "270000000"),
			array("200", "404000000"),
			array("210", "540000000"),
			array("220", "670000000"),
			array("230", "1500000000"),
			array("240", "6500000000"),
			array("250", "13000000000"),
			array("260", "67500000000"),		
		);
		
		foreach ($aCifraAfaceri as $key => $value) {
			foreach ($aCifre as $key2 => $value2) {
				$this->db->set($value,$value2[1]);
				$this->db->where($value,$value2[0]);
				$this->db->update("persons");
				echo $this->db->last_query()."<br>";
			}
		}
		
	}

}

